package com.cuicuisha.gxyp.manager.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.cuicuisha.gxyp.manager.mapper.CategoryMapper;
import com.cuicuisha.gxyp.model.vo.product.CategoryExcelVo;
import com.google.errorprone.annotations.Var;
import lombok.NoArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

/**
 * ClassName: CategoryReadListener
 * Description:
 *
 * @Author 脆脆鲨
 * @Create 2024-09-15 12:03
 * @Version 1.0
 */
@NoArgsConstructor
public class CategoryReadListener extends AnalysisEventListener<CategoryExcelVo> {

    List<CategoryExcelVo> categoryExcelVos = new ArrayList<>();

    @Autowired
    private CategoryMapper categoryMapper;

    public CategoryReadListener(CategoryMapper categoryMapper) {
        this.categoryMapper = categoryMapper;
    }

    @Override
    public void invoke(CategoryExcelVo categoryExcelVo, AnalysisContext analysisContext) {
        categoryExcelVos.add(categoryExcelVo);
        if (categoryExcelVos.size() >= 100){
            categoryMapper.insertBatch(categoryExcelVos);
            categoryExcelVos.clear();
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        categoryMapper.insertBatch(categoryExcelVos);
        categoryExcelVos.clear();
    }
}
